package com.aijk.web.admin.controller.ehm.dept;


import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.api.EhmCompanyDeptApi;
import com.aijk.ehm.api.EhmResidentApi;
import com.aijk.ehm.model.EhmResident;
import com.aijk.ehm.model.dto.EhmCompanyDeptDto;
import com.aijk.ehm.model.qo.EhmCompanyDeptQo;
import com.aijk.ehm.model.qo.EhmCompanyQo;
import com.aijk.ehm.model.qo.EhmResidentQo;
import com.aijk.ehm.model.vo.EhmCompanyDeptVo;
import com.aijk.ehm.model.vo.EhmCompanyVo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;


/**
 * 体检中心Controller
 */
@Controller
@RequestMapping("/admin/ehm/dept")
public class DeptController extends BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private EhmCompanyDeptApi ehmCompanyDeptApi;

    @Autowired
    private EhmResidentApi ehmResidentApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list_view(Model model) {
        if(isSubCompany()){
            if(isCompany()){
                return "error/no_permissions";
            }
            return "ehm/dept/list";
        }else{
            return "error/no_permissions";
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Integer draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        EhmCompanyDeptQo qo = DataTableUtil.fillQo(skey,EhmCompanyDeptQo.class);
        qo.setCompanyId(getCurrentUser().getCompanyId());
        draw += 1;
        PageInfo<EhmCompanyDeptVo> pageInfo = ehmCompanyDeptApi.page(qo);
        iTotalRecords =pageInfo.getTotal();
        RetMsg regMsg = new RetMsg();

        regMsg.put("data", pageInfo.getList());
        regMsg.put("draw", draw);
        regMsg.put("recordsTotal", iTotalRecords);
        regMsg.put("recordsFiltered", iTotalRecords);

        return regMsg;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add_view(Model model) {
        model.addAttribute("method","add");
        return "ehm/dept/edit";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg add(@RequestParam(value = "id") Integer id, @Valid EhmCompanyDeptVo data) {
        try {
            data.setCompanyId(getCurrentUser().getCompanyId());
            data.setStatus((byte)10);
            EhmCompanyDeptDto entity = PojoUtil.convert(data,EhmCompanyDeptDto.class);
            ehmCompanyDeptApi.save(entity);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String update_view(@RequestParam(value = "id") Long id,Model model) {
        EhmCompanyDeptVo entity = ehmCompanyDeptApi.findOne(id);
        model.addAttribute("data",entity);
        model.addAttribute("method","edit");
        return "ehm/dept/edit";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg update(@RequestParam(value = "id") Integer id, @Valid EhmCompanyDeptVo data) {
        try {
            EhmCompanyDeptDto entity = PojoUtil.convert(data,EhmCompanyDeptDto.class);
            ehmCompanyDeptApi.update(entity);

            EhmResident ehmResident = new EhmResident();
            ehmResident.setDept(entity.getTitle());
            EhmResidentQo ehmResidentQo = new EhmResidentQo();
            ehmResidentQo.setDeptId(entity.getId());
            ehmResidentApi.updateDeptName(ehmResident , ehmResidentQo);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/open", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg open(@RequestParam(value = "id") Long id) {
        try {
//            EhmCompanyDeptApi.deleteById(id);
            EhmCompanyDeptVo entity = ehmCompanyDeptApi.findOne(id);
            if(null != entity){
                if(entity.getStatus() != null){
                    int state = entity.getStatus() & 0xff;
                    if(state == 10){
                        entity.setStatus((byte)20);
                    }else{
                        entity.setStatus((byte)10);
                    }
                }else{
                    entity.setStatus((byte)10);
                }
                EhmCompanyDeptDto dto = PojoUtil.convert(entity , EhmCompanyDeptDto.class );
                ehmCompanyDeptApi.update(dto);
            }
        }catch (ErrorCodeException e){
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg del(@RequestParam(value = "id") Long id) {
        try {
            ehmCompanyDeptApi.deleteById(id);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            return RetMsg.failed();
        }
        return RetMsg.success();
    }

    @RequestMapping(value = "/validations", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg validation(String validation , Long id) {
        RetMsg msg = new RetMsg();
        try {
            EhmCompanyDeptQo qo = new EhmCompanyDeptQo();
            qo.setSn(validation);
            List<EhmCompanyDeptVo> vo = ehmCompanyDeptApi.list(qo);

            if(id == 0){
                if(vo != null && vo.size() > 0){
                    msg.put("msg" , "该编码已被占用");
                }else{
                    msg.put("msg" , "可以使用该编码");
                }
            }else{
                if(vo != null && vo.size() > 0){
                    if(id == vo.get(0).getId() ){
                        msg.put("msg" , "可以使用该编码");
                    }else{
                        msg.put("msg" , "该编码已被占用");
                    }
                }else{
                    msg.put("msg" , "可以使用该编码");
                }
            }
            msg.put("code" , 0);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            msg.put("code" , -1);
        }
        return msg;
    }
}
